package com.mentalhealthplatform.schoolV1.controller;

import com.mentalhealthplatform.global.log.manager.ManagerLogAction;
import com.mentalhealthplatform.schoolV1.dto.StudentDTO;
import com.mentalhealthplatform.schoolV1.service.StudentServiceV1;
import com.mentalhealthplatform.utils.PagedResponse;
import com.mentalhealthplatform.utils.stringUtils.ConvertEmptyToNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/v1/students")
public class StudentControllerV1 {

    @Autowired
    private StudentServiceV1 studentServiceV1;

    // 获取所有学生信息（分页、排序）
    @GetMapping
    public ResponseEntity<PagedResponse<StudentDTO>> getStudents(
            @RequestParam(required = false) @ConvertEmptyToNull String name,
            @RequestParam(required = false) @ConvertEmptyToNull String major,
            @RequestParam(required = false) @ConvertEmptyToNull String college,
            @RequestParam(required = false) @ConvertEmptyToNull String gender,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate,
            @RequestParam(defaultValue = "1") int page,          // 默认页码为1
            @RequestParam(defaultValue = "10") int pageSize,     // 默认每页大小为10
            @RequestParam(required = false) String sortField,    // 排序字段
            @RequestParam(required = false) String sortOrder) {  // 排序顺序

        // 设置分页和排序信息，确保排序字段和顺序可选
        Sort sort = Sort.by(Sort.Order.asc(sortField != null ? sortField : "id"));
        if ("desc".equalsIgnoreCase(sortOrder)) {
            sort = Sort.by(Sort.Order.desc(sortField != null ? sortField : "id"));
        }

        // 创建 Pageable 对象，包含分页和排序信息
        Pageable pageable = PageRequest.of(page - 1, pageSize, sort);

        // 调用 Service 层获取分页数据
        PagedResponse<StudentDTO> result = studentServiceV1.getStudents(
                name, major, college, gender, startDate, endDate, pageable);

        return ResponseEntity.ok(result);
    }

    // 根据学生ID获取学生信息
    @GetMapping("/{studentId}")
    public ResponseEntity<StudentDTO> getStudentById(@PathVariable UUID studentId) {
        StudentDTO studentDTO = studentServiceV1.getStudentById(studentId);
        return ResponseEntity.ok(studentDTO);
    }

    // 根据学号获取学生信息
    @GetMapping("/number/{number}")
    public ResponseEntity<StudentDTO> getStudentByNumber(@PathVariable String number) {
        StudentDTO studentDTO = studentServiceV1.getStudentByNumber(number);
        return ResponseEntity.ok(studentDTO);
    }

    // 添加新学生
    @PostMapping
    @ManagerLogAction(module = "学生档案", operationType = "添加")
    public ResponseEntity<StudentDTO> addStudent(@RequestBody StudentDTO studentDTO) {
        StudentDTO newStudent = studentServiceV1.addStudent(studentDTO);
        return ResponseEntity.status(201).body(newStudent);
    }

    // 更新学生信息
    @PutMapping("/{studentId}")
    public ResponseEntity<StudentDTO> updateStudent(
            @PathVariable UUID studentId, @RequestBody StudentDTO studentDTO) {
        StudentDTO updatedStudent = studentServiceV1.updateStudent(studentId, studentDTO);
        return ResponseEntity.ok(updatedStudent);
    }

    // 删除学生
    @DeleteMapping("/{studentId}")
    public ResponseEntity<Void> deleteStudent(@PathVariable UUID studentId) {
        studentServiceV1.deleteStudent(studentId);
        return ResponseEntity.noContent().build();
    }

    // 批量删除教师
    @PostMapping("/delete")
    public ResponseEntity<Void> batchDeleteStudents(@RequestBody List<UUID> studentIds) {
        studentServiceV1.batchDeleteStudents(studentIds);
        return ResponseEntity.noContent().build();
    }
}
